<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
    <link rel="stylesheet" href="../../styles/common.css" />
    <link rel="stylesheet" href="../../styles/page.css" />
</head>
<body>
<div class="addBrand-container" id="member-add-app">
    <div class="container">
        <el-form
                ref="ruleForm"
                :model="ruleForm"
                :rules="rules"
                :inline="false"
                label-width="180px"
                class="demo-ruleForm"
        >
            <el-form-item label="订单编码:" prop="billcode">
                <el-input v-model="ruleForm.billcode" placeholder="请输入订单编码" maxlength="20"/>
            </el-form-item>

            <el-form-item label="商品名称:" prop="productname">
                <el-input v-model="ruleForm.productname" placeholder="请输入商品名称" maxlength="20"/>
            </el-form-item>

            <el-form-item label="商品描述:" prop="productdesc">
                <el-input v-model="ruleForm.productdesc" placeholder="请输入商品描述" maxlength="50"/>
            </el-form-item>

            <el-form-item label="商品单位:" prop="productunit">
                <el-input v-model="ruleForm.productunit" placeholder="请输入商品单位" maxlength="10"/>
            </el-form-item>

            <el-form-item label="商品数量:" prop="productcount">
                <el-input v-model="ruleForm.productcount" placeholder="请输入商品数量" maxlength="20"/>
            </el-form-item>

            <el-form-item label="商品总额:" prop="totalprice">
                <el-input v-model="ruleForm.totalprice" placeholder="请输入商品总额" maxlength="20"/>
            </el-form-item>


            <el-form-item label="支付状态" prop="ispayment">
                <el-radio-group v-model="ruleForm.ispayment">
                    <el-radio :label="0">未支付</el-radio>
                    <el-radio :label="1">已支付</el-radio>
                </el-radio-group>
            </el-form-item>

            <el-form-item label="供应商:" prop="providerid">
                <el-select v-model="ruleForm.providerid" placeholder="请选择供应商" style="width: 250px">
                    <el-option label="未选择供应商" value=""></el-option>
                    <el-option v-for="provider in providerDto" :key="provider.id" :label="provider.proname" :value="provider.id" >
                    </el-option>
                </el-select>
            </el-form-item>

            <div class="subBox address">
                <el-form-item>
                    <el-button  @click="goBack4()">
                        取消
                    </el-button>
                    <el-button
                            type="primary"
                            @click="submitBillForm('ruleForm', false)"
                    >
                        保存
                    </el-button>
                    <el-button
                            v-if="actionType == 'add'"
                            type="primary"
                            class="continue"
                            @click="submitBillForm('ruleForm', true)"
                    >
                        保存并继续添加
                    </el-button>
                </el-form-item>
            </div>
        </el-form>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>

<script src="../../api/bill.js"></script>
<script src="../../js/validate.js"></script>
<script src="../../js/index.js"></script>
<script>
    new Vue({
        el: '#member-add-app',
        data() {
            return {
                id: '',
                actionType : '',
                providerDto: {
                    id : '',
                    proname: '',
                },
                ruleForm : {
                    billcode: '',
                    productname: '',
                    productdesc: '',
                    productunit: '',
                    productcount: '',
                    totalprice: '',
                    ispayment: 0,
                    providerid: '',
                    proname: ''
                }
            }
        },
        computed: {
            rules () {
                return {
                    providerid: [
                        {
                            required: true, 'validator': NotEmpty, trigger: 'blur'
                        }
                    ],
                }
            }
        },
        created() {
            this.id = requestUrlParam('id')
            this.actionType = this.id ? 'edit' : 'add'
            if (this.id) {
                this.init()
            }
        },
        mounted() {
            this.getProviderDto();
        },
        methods: {
            async init () {
                queryBillById(this.id).then(res => {
                    if (String(res.code) === '1') {
                        this.ruleForm = res.data
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                })
            },
            submitBillForm (formName, st) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.actionType === 'add') {
                            const params = {
                                ...this.ruleForm,
                            }
                            addBill(params).then(res => {
                                if (res.code === 1) {
                                    this.$message.success('订单添加成功！')
                                    if (!st) {
                                        this.goBack4()
                                    } else {
                                        this.ruleForm = {
                                            billcode: '',
                                            productname: '',
                                            productdesc: '',
                                            productunit: '',
                                            productcount: '',
                                            totalprice: '',
                                            ispayment: 0,
                                            providerid: '',
                                            proname:''
                                        }
                                    }
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        } else {
                            const params = {
                                ...this.ruleForm,
                            }
                            editBill(params).then(res => {
                                if (res.code === 1) {
                                    this.$message.success('订单信息修改成功！')
                                    this.goBack4()
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        }
                    } else {
                        console.log('error submit!!')
                        return false
                    }
                })
            },
            getProviderDto () {
                return $axios({
                    url: '/provider/all/providerDto',
                    method: 'get',
                }).then(response => {
                    if (response.data && response.data.length) {
                        this.providerDto = response.data;
                    }
                }).catch(error => {
                    console.error(error);
                });
            },
            goBack4(){
                window.parent.menuHandle({
                    id: '4',
                    url: '/backend/page/bill/list.html',
                    name: '订单管理'
                },false)
            }
        }
    })
</script>
</body>
</html>